package com.stx.WORK;

import java.io.*;
import java.util.Arrays;

/**
 * - @Author: XRH。
 * - @Date: 2025-10-13。
 * - @Description: 序列化与反序列化。
 * - @PROJECT:00
 **/
public class SerializationDemo {
    public static void main(String[] args) {
        Person person = new Person("XHR", 22, Arrays.asList("Java"));

        // 序列化
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            oos.writeObject(person);
            byte[] serializedData = baos.toByteArray();
            System.out.println("序列化结果: " + Arrays.toString(serializedData));

            // 反序列化
            try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
                 ObjectInputStream ois = new ObjectInputStream(bais)) {
                Person deserializedPerson = (Person) ois.readObject();
                System.out.println("反序列化结果: " + deserializedPerson);
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}